package com.example.su_shi_tan_app.scan

import android.Manifest
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.su_shi_tan_app.R
import com.example.su_shi_tan_app.databinding.ScanFrameFragmentBinding
import com.example.su_shi_tan_bas_lib.base.BaseLazyDataBindingFragment

import com.example.su_shi_tan_core.arouter.AppARouter
import com.example.su_shi_tan_core.arouter.AppARouterParams
import com.example.su_shi_tan_core.extentions.goneAlphaAnimation
import com.example.su_shi_tan_core.extentions.visibleAlphaAnimation
import com.example.su_shi_tan_core.scan.ScanResListener
import com.example.su_shi_tan_core.utils.HiDataBus
import com.permissionx.guolindev.PermissionX

@Route(path = AppARouter.ScanFrameFragment)
class ScanFrameFragment : BaseLazyDataBindingFragment<ScanFrameFragmentBinding>(), ScanResListener {

    private val mScanFragment by lazy {
        ScanFragment.newInstance()
    }

    override fun layoutId(): Int {
        return R.layout.scan_frame_fragment
    }

    override fun lazyInit() {
        mScanFragment.setScanResListener(this)

    }

    override fun initView(view: View) {

    }

    override fun onEnterAnimationEnd(savedInstanceState: Bundle?) {
        super.onEnterAnimationEnd(savedInstanceState)
        PermissionX.init(this)
            .permissions(Manifest.permission.CAMERA)
            .onExplainRequestReason { scope, deniedList ->
                val msg = "获取内部存储和摄像头权限"
                val positive = "确定"
                val negative = "取消"
                scope.showRequestReasonDialog(deniedList, msg, positive, negative)
            }.onForwardToSettings { scope, deniedList ->
                val msg = "获取内部存储和摄像头权限"
                val positive = "确定"
                val negative = "取消"
                scope.showForwardToSettingsDialog(deniedList, msg, positive, negative)
            }.request { allGranted, _, deniedList ->
                if (!allGranted || deniedList.isNotEmpty()) {

                    this.onBackPressedSupport()
                } else {
                    loadSan()
                }

            }
    }

    private fun loadSan() {
        viewBinder.frameLayout.visibleAlphaAnimation()
        viewBinder.mViewLoading.goneAlphaAnimation()
        loadRootFragment(R.id.frameLayout, mScanFragment)
    }

    override fun onResStr(resStr: String) {
        HiDataBus.with<String>(AppARouterParams.ScanFrameParams)
            .postStickyData(resStr)
    }


}